package com.self.av;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.Switch;

import androidx.appcompat.app.AppCompatActivity;

public class CustomNodeActivity extends AppCompatActivity {

    private SharedPreferences sharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom_node);

        sharedPreferences = getSharedPreferences("VideoPlayerPrefs", MODE_PRIVATE);

        // 获取所有开关
        Switch[] switches = new Switch[]{
                findViewById(R.id.node0),
                findViewById(R.id.node1),
                findViewById(R.id.node2),
                findViewById(R.id.node3),
                findViewById(R.id.node4),
                findViewById(R.id.node5),
                findViewById(R.id.node6),
                findViewById(R.id.node7),
                findViewById(R.id.node8)
        };

        // 为每个开关添加点击监听器
        for (Switch switchView : switches) {
            boolean isEnabled = sharedPreferences.getBoolean(switchView.getTag() + "", true);
            switchView.setChecked(isEnabled);
            switchView.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    // 更新 SharedPreferences 中的状态
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    editor.putBoolean(buttonView.getTag() + "", isChecked);
                    editor.putBoolean("shouldProcessFrame", true); // 标记需要处理帧
                    editor.apply();
                }
            });
        }


        // 获取返回按钮并添加点击监听器
        Button backButton = findViewById(R.id.backButton);
        backButton.setOnClickListener(v -> finish()); // 点击返回按钮关闭当前活动

        // 获取全选按钮并添加点击监听器
        Button selectAllButton = findViewById(R.id.selectAllButton);
        selectAllButton.setOnClickListener(v -> {
            SharedPreferences.Editor editor = sharedPreferences.edit();
            for (Switch switchView : switches) {
                switchView.setChecked(true); // 设置所有开关为选中状态
                editor.putBoolean(switchView.getId() + "", true); // 更新 SharedPreferences
            }
            editor.putBoolean("shouldProcessFrame", true); // 标记需要处理帧
            editor.apply();
        });

        // 获取取消全选按钮并添加点击监听器
        Button cancelSelectAllButton = findViewById(R.id.cancelSelectAllButton);
        cancelSelectAllButton.setOnClickListener(v -> {
            SharedPreferences.Editor editor = sharedPreferences.edit();
            for (Switch switchView : switches) {
                switchView.setChecked(false); // 设置所有开关为未选中状态
                editor.putBoolean(switchView.getId() + "", false); // 更新 SharedPreferences
            }
            editor.putBoolean("shouldProcessFrame", true); // 标记需要处理帧
            editor.apply();
        });

    }
}